﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EvaluationSystem.Exceptions;
using System.Data;
using System.Web.UI;

namespace EvaluationSystem.DAO
{
    public class Summary
    {
        public int Id { get; set; }
        public string TabName { get; set; }
        public string SummaryName { get; set; }
        public string GroupName { get; set; }
    }
    public class SummaryManager:DAO
    {

        public bool AddSummary(string tabId, string summaryName,string groupName)
        {
            if (CheckExistSummaryByName(summaryName, tabId, groupName))
            {
                throw new ExistSummaryException();
            }
            string insertIntoSummaryCommand = "insert into [Summary] ([TabId],[SummaryName],[GroupName]) values(@TabId, @SummaryName, @GroupName) ";
            Dictionary<string, object> parameters = new Dictionary<string, object>(3);
            parameters["TabId"] = tabId;
            parameters["SummaryName"] = summaryName;
            parameters["GroupName"] = groupName;
            return ExcuteCommandNonQuery(insertIntoSummaryCommand, parameters) > 0;
        }

        public bool CheckExistSummaryByName(string summaryName,string tabId, string groupName)
        {
            string selectSummaryByNameCommand = "select [Id] from [Summary] where [SummaryName]=@SummaryName and [TabId]=@TabId and [GroupName]=@GroupName";
            Dictionary<string, object> parameters = new Dictionary<string, object>(3);
            parameters["SummaryName"] = summaryName;
            parameters["TabId"] = tabId;
            parameters["GroupName"] = groupName;
            return ExcuteCommandDataTable(selectSummaryByNameCommand, parameters).Rows.Count > 0;
        }

        public DataTable GetAllSummary()
        {
            string selectAllSummaryCommand = "select [Summary].[Id],[TabName],[SummaryName], [Summary].[GroupName] from [Summary],[Tabs] where [Summary].[TabId]=[Tabs].[Id]";
            return ExcuteCommandDataTable(selectAllSummaryCommand, null);
        }

        public bool DeleteSummaryById(string id)
        {
            string deleteSummaryByIdCommand = "delete [Summary] where [Id]=@Id";
            Dictionary<string, object> parameters = new Dictionary<string, object>(1);
            parameters["Id"] = id;
            return ExcuteCommandNonQuery(deleteSummaryByIdCommand, parameters) > 0;
        }

        public int GetSummaryCount(Page page, bool isRefresh = false)
        {
            if (page.Session[Constants.SummaryCount] != null && !isRefresh)
            {
                return (int)page.Session[Constants.SummaryCount];
            }
            else
            {
                string getSummaryCountCommand = "select count([Id]) from [Summary] ";
                DataTable dt = ExcuteCommandDataTable(getSummaryCountCommand, null);
                int count = 0;
                if (dt.Rows.Count > 0)
                {
                    count = (int)dt.Rows[0][0];
                }
                return count;
            }
        }

        public bool UpdateSummary(string id, string summaryName, string tabId, string groupName)
        {
            if (CheckExistSummaryNameExceptMe(id,summaryName))
            {
                throw new ExistSummaryException();
            }
            string updateSummaryCommand = "update [Summary] set [TabId]=@TabId, [SummaryName]=@SummaryName, [GroupName]=@GroupName where [Id]=@Id";
            Dictionary<string, object> parameters = new Dictionary<string, object>(4);
            parameters["Id"] = id;
            parameters["TabId"] = tabId;
            parameters["SummaryName"] = summaryName;
            parameters["GroupName"] = groupName;
            return ExcuteCommandNonQuery(updateSummaryCommand, parameters) > 0;
        }

        protected bool CheckExistSummaryNameExceptMe(string id, string summaryName)
        {
            string selectSummaryExceptMeCommand = "select [Id] from [Summary] where [SummaryName]=@SummaryName and [Id]<>@Id";
            Dictionary<string, object> parameters = new Dictionary<string, object>(2);
            parameters["SummaryName"] = summaryName;
            parameters["Id"] = id;
            return ExcuteCommandDataTable(selectSummaryExceptMeCommand, parameters).Rows.Count > 0;
        }
    }
}